﻿using System.Collections.Generic;
using Clipping.Infrastructure.Interfaces;
using Clipping.Infrastructure.Models;
namespace Clipping.SearchEnginePageParsers
{
    public class BaiduNewsPageParser : ISearchEnginePageParse
    {
        public void Parse(string html, out List<News> news, out string nextPageUrl)
        {
            news = new List<News>();
            nextPageUrl = null;
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);

            var liNav = doc.CreateNavigator();

            var divContentSearch = liNav.Select("//div[@id='content_left']/ul/li[@class='result']");
            while (divContentSearch.MoveNext())
            {
                News n = new News();

                var li = divContentSearch.Current;

                var link = li.SelectSingleNode("descendant::h3[@class='c-title']/a");
                if (link != null)
                {
                    n.Title = link.Value;
                    n.Url = link.GetAttribute("href", "");
                }

                var mediaAndTime = li.SelectSingleNode("descendant::span[@class='c-author']");
                if (mediaAndTime != null)
                {
                    string mt = mediaAndTime.Value;
                    if (!string.IsNullOrEmpty(mt))
                    {

                        mt = System.Web.HttpUtility.HtmlDecode(mt);
                        mt = mt.Trim();
                        int spaceIndex = System.Text.RegularExpressions.Regex.Match(mt, @"\s").Index;
                        if (spaceIndex > 0)
                        {
                            n.Media = mt.Substring(0, spaceIndex);
                            n.PubDate = mt.Substring(spaceIndex + 1);
                        }
                    }
                }

                var summary = li.SelectSingleNode("descendant::div[@class='c-summary']");
                if (summary != null)
                {


                    var sameNews = summary.Select("descendant::a[@class='c-more_link']");
                    string sameNewsStr = string.Empty;
                    while (sameNews.MoveNext())
                    {
                        if (sameNews.Current.Value.Contains("条相同新闻"))
                        {
                            n.SameNewsUrl = sameNews.Current.GetAttribute("href", "");
                            sameNewsStr = sameNews.Current.Value;
                            break;
                        }
                    }


                    n.Summary = summary.Value;
                    if (!string.IsNullOrEmpty(summary.Value))
                    {
                        if (!string.IsNullOrEmpty(sameNewsStr))
                        {
                            n.Summary = n.Summary.Replace(sameNewsStr, "");
                            n.Summary = n.Summary.Replace("&nbsp;-&nbsp;百度快照", "");
                        }
                        else
                        {
                        n.Summary = n.Summary.Replace("百度快照", "");
                        }
                    }
                }



                news.Add(n);
            }





            var divTitleSearch = liNav.Select("//div[@id='content_left']/ul/li[@class='result title' or @class='result title titlelast']");

            while (divTitleSearch.MoveNext())
            {
                News n = new News();

                var li = divTitleSearch.Current;

                var link = li.SelectSingleNode("descendant::h3[@class='c-title']/a");
                if (link != null)
                {
                    n.Title = link.Value;
                    n.Url = link.GetAttribute("href", "");
                }

                var mediaAndTime = li.SelectSingleNode("child::span[@class='c-author']");
                if (mediaAndTime != null)
                {
                    string mt = mediaAndTime.Value;
                    if (!string.IsNullOrEmpty(mt))
                    {
                        mt = System.Web.HttpUtility.HtmlDecode(mt);
                        mt = mt.Trim();
                        int spaceIndex = System.Text.RegularExpressions.Regex.Match(mt, @"\s").Index;
                        if (spaceIndex > 0)
                        {
                            n.Media = mt.Substring(0, spaceIndex);
                            n.PubDate = mt.Substring(spaceIndex + 1);
                        }
                    }
                }


                var sameNews = li.Select("descendant::a[@class='c-more_link']");
                while (sameNews.MoveNext())
                {
                    if (sameNews.Current.Value.Contains("条相同新闻"))
                    {
                        n.SameNewsUrl = sameNews.Current.GetAttribute("href", "");

                        break;
                    }
                }

                news.Add(n);

            }



            var nextPage = liNav.SelectSingleNode("//p[@id='page']/a[last()]");
            if (nextPage != null && nextPage.Value != null && nextPage.Value.Contains("下一页"))
            {
                nextPageUrl = nextPage.GetAttribute("href", "");
            }
        }
    }
}
